package figure8;

import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.UnsupportedAudioFileException;

public class WAVRecorder implements Recorder {
	private Project project;
	private LoopTiming timing;
	
	private static Mixer mixer;
	private static AudioFormat format;
	private static DataLine.Info info;
	
	static {
		Mixer.Info[] mixers = AudioSystem.getMixerInfo();
		
		/*for (int i = 0; i < mixers.length; ++i) {
			Mixer.Info info = mixers[i];
			System.out.println(info.getName() + ": " + info.getDescription());
		}*/
		
		mixer = AudioSystem.getMixer(mixers[0]);
		
		float sampleRate = 44100.0F;
		// 8000,11025,16000,22050,44100
		int sampleSizeInBits = 16;
		// 8,16
		int channels = 1;
		// 1,2
		boolean signed = true;
		// true,false
		boolean bigEndian = false;
		// true,false
		format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
				bigEndian);
		
		info = new DataLine.Info(Clip.class, format);
	}
	
	public WAVRecorder(Project project, LoopTiming timing) {
		this.project = project;
		this.timing = timing;
	}
	
	@Override
	public WAVTrack record() {
		return null;
	}
	
	private void finalise() {
		Clip clip = null;
		
		try {
			clip = (Clip) mixer.getLine(info);
		} catch (LineUnavailableException e) {
			e.printStackTrace();
		}
		
		Track track = null;
		try {
			track = new WAVTrack((Clip) clip, null, timing);
		} catch (LineUnavailableException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedAudioFileException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (track != null) {
			this.project.addTrack(track);
		}
	}
}
